/**
 * @param example axios实例
 */
class requestMethod {
  constructor(example) {
    this.example = example
  }

  get(url, data, config = {}) {
    return new Promise((resolve, reject) => {
      this.example({
        method: 'get',
        url,
        params: data,
        ...config
      })
        .then(request => {
          resolve(request)
        })
        .catch(error => {
          reject(error)
        })
    })
  }

  post(url, data = {}) {
    return new Promise((resolve, reject) => {
      this.example({
        method: 'post',
        url,
        data,
        responseType: 'json',
      })
        .then(request => {
          resolve(request)
        })
        .catch(error => {
          reject(error)
        })
    })
  }

  delete(url, data = {}) {
    return new Promise((resolve, reject) => {
      this.example({
        method: 'delet',
        url,
        params: data,
        responseType: 'json',
      })
        .then(request => {
          resolve(request)
        })
        .catch(error => {
          reject(error)
        })
    })
  }

  postupload(url, data = {}, cancelToken, callback) {
    return new Promise((resolve, reject) => {
      this.example({
        method: 'post',
        url,
        data,
        timeout: 10 * 64 * 1000,
        cancelToken,
        onUploadProgress(progressEvent) {
          // 上传进度条事件
          // console.log('上传进度条事件', progressEvent)
          if (progressEvent.lengthComputable) {
            callback(progressEvent)
          }
        },
      })
        .then(request => {
          resolve(request)
        })
        .catch(error => {
          reject(error)
        })
    })
  }
}

export default requestMethod
